package com.hankol.winecellar.utils

import android.graphics.Color
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import com.baselibrary.utils.SpUtils
import com.hankol.winecellar.adapter.EditWineryUploadImageVideoAdapter
import com.hankol.winecellar.adapter.UploadImageVideoAdapter
import com.hankol.winecellar.bean.WineryCoverBean
import java.util.*

/**
 * @author 关忆北丶
 * @description:
 * @date :2020/9/24 17:47
 */
object RecyUtils {
/*上传图片视频拖拽排序*/

    fun itemTouchHelper(adapter: UploadImageVideoAdapter, list: ArrayList<String>, maxCount: Int): ItemTouchHelper {
        return ItemTouchHelper(object : ItemTouchHelper.Callback() {
            override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder): Int {
                val dragFlags = ItemTouchHelper.UP or ItemTouchHelper.DOWN or
                        ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT
                val swipeFlags = 0
                return makeMovementFlags(dragFlags, swipeFlags)
            }

            override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean {
                SpUtils.getInstance().putDynamicDrag()
                //得到当拖拽的viewHolder的Position
                val fromPosition = viewHolder.adapterPosition
                //拿到当前拖拽到的item的viewHolder
                val toPosition = target.adapterPosition
                if (maxCount != list.size && toPosition == list.size) {
                    return true
                }
                if (fromPosition < toPosition) {//左边图片往右拖动
                    for (i in fromPosition until toPosition) {
                        if (maxCount != list.size) {
                            if (i + 1 == list.size)
                                continue
                        }
                        Collections.swap(list, i, i + 1)
                    }
                } else {//右边图片往左拖动
                    for (i in fromPosition downTo toPosition + 1) {
                        if (maxCount != list.size) {
                            if (i - 1 == -1)
                                continue
                        }
                        Collections.swap(list, i, i - 1)
                    }
                }
                adapter.notifyItemMoved(fromPosition, toPosition)
                return true
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {

            }

            override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) {
                super.onSelectedChanged(viewHolder, actionState)

                if (actionState != ItemTouchHelper.ACTION_STATE_IDLE)
                    viewHolder!!.itemView.setBackgroundColor(Color.LTGRAY)
            }

            override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
                super.clearView(recyclerView, viewHolder)

                viewHolder.itemView.setBackgroundColor(0)
                adapter.setData(list)
                adapter.notifyDataSetChanged()
            }

            //重写拖拽不可用
            override fun isLongPressDragEnabled(): Boolean {

                return false
            }


        })
    }

/*上传图片视频拖拽排序*/

    fun itemTouchHelper4EditWinery(adapter: EditWineryUploadImageVideoAdapter, list: List<WineryCoverBean>, maxCount: Int): ItemTouchHelper {
        return ItemTouchHelper(object : ItemTouchHelper.Callback() {
            override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder): Int {
                val dragFlags = ItemTouchHelper.UP or ItemTouchHelper.DOWN or
                        ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT
                val swipeFlags = 0
                return makeMovementFlags(dragFlags, swipeFlags)
            }

            override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean {
                //得到当拖拽的viewHolder的Position
                val fromPosition = viewHolder.adapterPosition
                //拿到当前拖拽到的item的viewHolder
                val toPosition = target.adapterPosition
                if (maxCount != list.size && toPosition == list.size) {
                    return true
                }
                if (fromPosition < toPosition) {//左边图片往右拖动
                    for (i in fromPosition until toPosition) {
                        if (maxCount != list.size) {
                            if (i + 1 == list.size)
                                continue
                        }
                        Collections.swap(list, i, i + 1)
                        adapter.notifyItemMoved(fromPosition, toPosition)
                    }
                } else {//右边图片往左拖动
                    for (i in fromPosition downTo toPosition + 1) {
                        if (maxCount != list.size) {
                            if (i - 1 == -1)
                                continue
                        }
                        if (i - 1 != 0) {
                            Collections.swap(list, i, i - 1)
                            adapter.notifyItemMoved(fromPosition, toPosition)
                        } else {
                            if (list[i - 1].type != 1) {
                                Collections.swap(list, i, i - 1)
                                adapter.notifyItemMoved(fromPosition, toPosition)
                            }
                        }
                    }
                }
                return true
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {

            }

            override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) {
                super.onSelectedChanged(viewHolder, actionState)

                if (actionState != ItemTouchHelper.ACTION_STATE_IDLE)
                    viewHolder!!.itemView.setBackgroundColor(Color.LTGRAY)
            }

            override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
                super.clearView(recyclerView, viewHolder)

                viewHolder.itemView.setBackgroundColor(0)
                adapter.setData(list)
                adapter.notifyDataSetChanged()
            }

            //重写拖拽不可用
            override fun isLongPressDragEnabled(): Boolean {

                return false
            }


        })
    }

}
